package com.chii.image.controller;


import com.chii.image.po.ResponeInfo;
import com.chii.image.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import okio.Timeout;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Time;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(value = "图片上传controller", tags = {"图片上传"})
@RestController
public class UploadController {
    private Logger logger = LogManager.getLogger(UploadController.class);
    @Value("${localhost.localimage}")
    private String imageHost;

    @Value("${localhost.python}")
    private String pythonHost;
    @Autowired
    private ImageService imageService;



    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "updateImage", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponeInfo updateImage(@ApiParam(name = "file", value = "上传图片文件") @RequestParam("file") MultipartFile file,
                                   @ApiParam(name = "fileName", value = "上传图片名字") @RequestParam("fileName") String fileName,
                                   @ApiParam(name = "type", value = "上传方式") @RequestParam("type") String type) throws IOException, InterruptedException {
        ResponeInfo responeInfo = new ResponeInfo();
        if (file == null) {
            responeInfo.setMsg("没有文件上传");
            return responeInfo;
        }
        String inFile = null;
        if ("ftp".equals(type)) {
            //保存图片ftp
            inFile = imageService.saveImageFTP(file, fileName);
        } else if ("local".equals(type)){
            inFile = imageService.saveImageLocal(file, fileName);
        }else if ("qiniu".equals(type)){
            inFile = imageService.saveImageQiNiu(file, fileName);
        }
        //插入历史记录
        responeInfo.setData(inFile);
        return responeInfo;
    }
    @ApiOperation(value = "上传Base64文件")
    @RequestMapping(value = "updateBase64Image", method = RequestMethod.POST)
    public ResponeInfo updateBase64Image(@ApiParam(name = "base64", value = "上传图片文件") @RequestParam("base64") String base64,
                                   @ApiParam(name = "fileName", value = "上传图片名字") @RequestParam("fileName") String fileName,
                                   @ApiParam(name = "type", value = "上传方式") @RequestParam("type") String type) throws IOException {
        ResponeInfo responeInfo = new ResponeInfo();
        if (base64 == null) {
            responeInfo.setMsg("没有文件上传");
            return responeInfo;
        }
        String inFile = null;
        if ("ftp".equals(type)) {
            //保存图片ftp
            inFile = imageService.saveBase64ImageFTP(base64, fileName);
        } else if ("local".equals(type)){
            inFile = imageService.saveBase64ImageLocal(base64, fileName);
        }else if ("qiniu".equals(type)){
            inFile = imageService.saveBase64ImageQiNiu(base64, fileName);
        }
        //插入历史记录
        responeInfo.setData(inFile);
        return responeInfo;
    }
}
